//
//  IndexViewController.swift
//  test_swift
//
//  Created by fangyuan ding on 2021/5/31.
//

import UIKit

class IndexViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        struct subexample {
            let decrementer: Int
            subscript(index: Int) -> Int {
                return decrementer / index
            }
        }
        let division = subexample(decrementer: 100)

        print("100 除以 9 等于 \(division[9])")
        print("100 除以 2 等于 \(division[2])")
        print("100 除以 3 等于 \(division[3])")
        print("100 除以 5 等于 \(division[5])")
        print("100 除以 7 等于 \(division[7])")
        
        
        class daysofaweek {
            private var days = ["Sunday", "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "saturday"]
            subscript(index: Int) -> String {
                get {
                    return days[index]   // 声明下标脚本的值
                }
                set(newValue) {
                    self.days[index] = newValue   // 执行赋值操作
                }
            }
        }
        var p = daysofaweek()

        print(p[0])
        print(p[1])
        print(p[2])
        print(p[3])
        
        var numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
        numberOfLegs["bird"] = 2

        print(numberOfLegs)
        
        
        struct Matrix {
            let rows: Int, columns: Int
            var print: [Double]
            init(rows: Int, columns: Int) {
                self.rows = rows
                self.columns = columns
                print = Array(repeating: 0.0, count: rows * columns)
            }
            subscript(row: Int, column: Int) -> Double {
                get {
                    return print[(row * columns) + column]
                }
                set {
                    print[(row * columns) + column] = newValue
                }
            }
        }
        // 创建了一个新的 3 行 3 列的Matrix实例
        var mat = Matrix(rows: 3, columns: 3)

        // 通过下标脚本设置值
        mat[0,0] = 1.0
        mat[0,1] = 2.0
        mat[1,0] = 3.0
        mat[1,1] = 5.0

        // 通过下标脚本获取值
        print("\(mat[0,0])")
        print("\(mat[0,1])")
        print("\(mat[1,0])")
        print("\(mat[1,1])")
    }
    



}
